package com.sophie.dao.impl;

import com.sophie.dao.BaseDAO;
import com.sophie.dao.IFollowDAO;
import com.sophie.entity.Follow;
import org.apache.poi.ss.formula.functions.T;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: Rowena
 * @CreateTime: 2024-09-11 08:56:18
 * @Version: 1.0
 */

public class FollowDAOImpl extends BaseDAO implements IFollowDAO {
    @Override
    public int addFollow(Follow follow) {
        try {
            String sql = "insert into follow(userid,followid,followtime) values(?,?,?)";
            int update = super.update(sql, follow.getUserid(), follow.getFollowid(), follow.getFollowtime());
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<T> selectFollowById(Integer id) {
        try {
            String sql = "select distinct followid from follow where userid=?";
            List<T> followsId = super.getAll(sql, T.class, id);
            return followsId;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int checkFollow(Integer id, Integer userid) {
        try {
            String sql = "select 1 from follow where followid=? and userid=? limit 1";
            Long i = super.getScalar(sql, id, userid);
            if (i > 0) {
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public long countFans(Integer id) {
        try {
            String sql = "select count(distinct userid) from follow where followid=?";
            Long i = super.getScalar(sql, id);
            if (i > 0) {
                return i;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public long countFollows(Integer id) {
        try {
            String sql = "select count(distinct followid) from follow where userid=?";
            Long i = super.getScalar(sql, id);
            if (i > 0) {
                return i;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
